package cn.ts.android.attendance.generator;

import cn.ts.android.attendance.constant.AttendanceConstant;
import cn.ts.android.attendance.data.AttenDanceDataHelper;
import cn.ts.android.attendance.data.AttenDanceDataHelperWithLock;
import cn.ts.android.attendance.listener.DataPoolListener;
import cn.ts.android.attendance.lock.MapWithLock;
import cn.ts.android.attendance.model.Attendence;
import cn.ts.android.attendance.model.UserDetail;
import cn.ts.android.attendance.sender.Sender;
import cn.ts.android.attendance.utils.Singleton;

import java.util.*;

public class DataBootrap implements AttendanceConstant {


    private MapWithLock<String,Integer> tMap;

    private DataPoolListener dataPoolListener;

    private List<UserDetail> userIds;

    private AttenDanceDataHelperWithLock attenDanceDataHelperWithLock;

    private Sender sender;

    public static DataBootrap getSingleton(){

       return  Singleton.getSingleton(DataBootrap.class);
    }


    public DataBootrap registerDataGenerator(DataPoolListener dataPoolListener){
        getSingleton().dataPoolListener=dataPoolListener;
        return  getSingleton();
    }


    public void notifyDataPoolListenerEvent(){

        getSingleton().dataPoolListener.reduce(getSingleton().tMap.get(initTotal));

    }

    public void notifyDataPoolListenerEvent20(){

        getSingleton().dataPoolListener.reduce20(getSingleton().tMap.get(initTotal));

    }

    /**
     * 开始测试
     */
    public  DataBootrap init(Integer total,DataPoolListener dataPoolListener,List<UserDetail> userIds){
         getSingleton().registerDataGenerator(dataPoolListener);
         getSingleton().attenDanceDataHelperWithLock=new AttenDanceDataHelperWithLock(new AttenDanceDataHelper());
         getSingleton().userIds=userIds;
         sender=new Sender();
         Map<String,Integer> map=new HashMap<>();
         map.put(initTotal,total);
         getSingleton().tMap=new MapWithLock<String,Integer>(map);
         return getSingleton();
    }


    public UserDetail randamUser(){
        Random random=new java.util.Random();
        int result=random.nextInt(getSingleton().userIds.size());
        return getSingleton().userIds.get(result);

    }

    public static String getRandomString(int length){
        String str="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0; i<length; ++i){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public void reduce10(){

       Integer t= getSingleton().tMap.get(initTotal);
       getSingleton().tMap.clear();
       getSingleton().tMap.put(initTotal,t-10);
       notifyDataPoolListenerEvent();

    }

    public void reduce20(){

        Integer t= getSingleton().tMap.get(initTotal);
        getSingleton().tMap.clear();
        getSingleton().tMap.put(initTotal,t-20);
        notifyDataPoolListenerEvent20();

    }

    public List<Attendence> init10Data( ){

        if (getSingleton().tMap.get(initTotal)==0){

            getSingleton().dataPoolListener.complite();

            return null;
        }

        List<Attendence> attendences=new ArrayList<>();
        for (int i=0;i<10;i++){
            UserDetail userDetail=randamUser();
            Attendence attendence=new Attendence(userDetail.getUserId(),serNum+getRandomString(5),null,null);
            attendences.add(attendence);
        }

        return attendences;
    }


    public List<Attendence> init20Data(){
        List<Attendence> attendences=new ArrayList<>();
        for (int i=0;i<20;i++){
            UserDetail userDetail=randamUser();
            Attendence attendence=new Attendence(userDetail.getUserId(),serNum+getRandomString(5),null,null);
            attendences.add(attendence);
        }
        return attendences;

    }

    /**
     * 这里可以是获取limit=20 serirequesNum =nulll 的考勤数据
     * 无论获取什么样的考勤数据，建议都是一个方法
     * 而不是直接提供数据库的入口
     * @return
     */
    public List<Attendence> getAttenDances(){

        return getSingleton().attenDanceDataHelperWithLock.get();
    }


    public void putAttenDances(List<Attendence> list){

        getSingleton().attenDanceDataHelperWithLock.putAttendance(list);
    }


    public void updateAttenDances(List<Attendence> list){

        getSingleton().attenDanceDataHelperWithLock.update(list);
    }


    /**
     *
     *
     * 以下要补充sender里面的方法
     */

}
